// pages/address-edit/address-edit.js
import areaData from '../../utils/area.js'
import WxValidate from '../../utils/wxValidate'

// 验证字段的规则
const rules = {
  receiver_name: {
    required: true,
  },
  receiver_phone: {
    required: true,
    tel: true,
  },
  receiver_province: {
    required: true,
  },
  receiver_address: {
    required: true,
  }
}

// 验证字段的提示信息，若不传则调用默认的信息
const messages = {
  receiver_name: {
    required: '请填写联系人',
  },
  receiver_phone: {
    required: '请填写联系电话',
    tel: '联系电话格式不正确',
  },
  receiver_province: {
    required: '请选择省市区',
  },
  receiver_address: {
    required: '请填写街道门牌号',
  }
}

const wxValidate = new WxValidate(rules, messages)

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {
      receiver_name: '',
      receiver_phone: '',
      receiver_province: '',
      receiver_city: '',
      receiver_area: '',
      receiver_address: '',
      receiver_default: false
    },
    errorMessage: {
      receiver_name: '',
      receiver_phone: '',
      receiver_province: '',
      receiver_address: ''
    },
    showAddress: false,
    areaList: areaData,
  },

  showAddressSheet() {
    this.setData({
      showAddress: true
    })
  },

  hideAddressSheet() {
    this.setData({
      showAddress: false
    })
  },

  confirmAddress(event) {
    const {values} = event.detail
    this.setData({
      'address.receiver_province': values[0].name,
      'address.receiver_city': values[1].name,
      'address.receiver_area': values[2].name,
      showAddress: false
    })
  },

  fieldChangeHandle(event) {
    const {name} = event.currentTarget.dataset
    const value = event.detail
    this.setData({
      [`address.${name}`]: value
    })
  },

  blurHandle(event) {
    const {name} = event.currentTarget.dataset
    const value = this.data.address[name]
    this.setData({
      [`errorMessage.${name}`]: wxValidate.validateField(name, value)
    })
  },

  submitHandle() {

    // 传入表单数据，调用验证方法
    if (!wxValidate.checkForm(this.data.address)) {
      let errorMessage = {}

      wxValidate.errorList.forEach(item => {
        errorMessage[item.param] = item.msg
      })
      this.setData({errorMessage})
    } else {
      this.requestSave()
    }
  },

  requestSave() {
    let data = {...this.data.address}
    data.receiver_default = data.receiver_default ? 'Y' : 'N' 
    wx.ajax.post(data.id ? 'updateAddress.action' : 'addAddress.action' , data, true).then(res => {
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];  //上一个页面

      if (prevPage.route == 'pages/shop-list/shop-list') {
        prevPage && prevPage.setData({
          address: data
        }, () => {
          wx.navigateBack({
            delta: 1,
          })
        })
      } else {
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {address} = options
    let title = "新增收货地址"

    if (address) {
      title = "编辑收货地址"
      address = JSON.parse(decodeURIComponent(address))
      address.receiver_default = address.receiver_default == "Y"
      this.setData({
        address
      })
    }

    wx.setNavigationBarTitle({
      title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})